import 'package:flutter/material.dart';
import 'package:xinxuan/common/address_bar.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:get/get.dart';
import 'package:xinxuan/common/network_image_util.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/controllers/order_controller/store_confirm_order_page_controller.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:common_utils/common_utils.dart';

class StoreConfirmOrderPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    Map<String, dynamic> argumentsMap = Get.arguments;
    int productId = argumentsMap['productId'];
    String headImage = argumentsMap['headImage'];
    String title = argumentsMap['title'];
    String price = argumentsMap['price'];
    int quantity = argumentsMap['quantity'];
    int skuId = argumentsMap['skuId'];
    String specStr = argumentsMap['specStr'];
    String subtotal = '${NumUtil.multiply(quantity, NumUtil.getDoubleByValueStr(price)!)}';
    return GetBuilder<StoreConfirmOrderPageController>(
      init: StoreConfirmOrderPageController(),
      builder: (controller) {
        return Scaffold(
          backgroundColor: pageBgColor,
          appBar: AppBarUtil(
            title: '确认订单',
            backgroundColor: pageBgColor,
          ),
          body: ScrollConfigurationCommon(child: ListView(
            children: [
              _addressBar(controller),
              SizedBox(height: 12.h),
              _goodsInfo(title, headImage, price, quantity, specStr, subtotal),
              SizedBox(height: 12.h),
              Container(
                color: bgWhiteColor,
                child: Column(
                  children: [
                    Container(
                      padding: EdgeInsets.symmetric(vertical: 8.h),
                      decoration: BoxDecoration(
                        border: Border(
                          bottom: BorderSide(
                            width: .5,
                            color: lineColor
                          )
                        )
                      ),
                      child: Column(
                        children: [
                          _goodsInfoItem('商品金额', '$subtotal'),
                          _goodsInfoItem('运费', '0.00'),
                          _goodsInfoItem('立减', '0.00'),
                        ],
                      ),
                    ),
                    Container(
                      height: 49.h,
                      padding: EdgeInsets.symmetric(horizontal: 16.w),
                      alignment: Alignment.centerRight,
                      child: Text.rich(
                        TextSpan(
                          text: '合计:  ',
                          style: TextStyle(
                            color: textBlackColor,
                            fontSize: 14.sp
                          ),
                          children: [
                            TextSpan(
                              text: '¥',
                              style: TextStyle(
                                color: mainTextColor,
                                fontSize: 14.sp,
                                fontWeight: FontWeight.bold
                              )
                            ),
                            TextSpan(
                              text: '$subtotal',
                              style: TextStyle(
                                color: mainTextColor,
                                fontSize: 18.sp,
                                fontWeight: FontWeight.bold
                              )
                            ),
                          ]
                        )
                      ),
                    )
                  ],
                ),
              ),
              SizedBox(height: 12.h),
              Container(
                padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 15.h),
                color: bgWhiteColor,
                alignment: Alignment.center,
                child: Text(
                  '此商品将正常发货，除质量问题，不支持退货。',
                  style: TextStyle(
                    color: mainTextColor,
                    fontSize: 11.sp
                  ),
                ),
              )
            ],
          )),
          bottomNavigationBar: BottomAppBar(
            elevation: 0,
            child: Container(
              height: 56.h,
              padding: EdgeInsets.symmetric(horizontal: 16.w),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text.rich(
                    TextSpan(
                      text: '¥',
                      style: TextStyle(
                        color: mainTextColor,
                        fontSize: 18.sp,
                        fontWeight: FontWeight.bold
                      ),
                      children: [
                        TextSpan(
                          text: '$subtotal',
                          style: TextStyle(
                            color: mainTextColor,
                            fontSize: 22.sp,
                            fontWeight: FontWeight.bold
                          )
                        )
                      ]
                    )
                  ),
                  Container(
                    width: 120.w,
                    child: ButtonUtil(
                      text: '提交订单', 
                      fontSize: 14.sp, 
                      borderRadius: 18.w, 
                      height: 36.h, 
                      onTop: () {
                        controller.submitOrder(productId, skuId, quantity);
                      }
                    ),
                  )
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  /// 地址栏
  Widget _addressBar(StoreConfirmOrderPageController controller) {
    return InkWellUtil.shakeProofInkWell(AddressBar(
      consignee: controller.addressItem.value.consignee ?? '', 
      mobile: controller.addressItem.value.mobile ?? '', 
      provinceStr: controller.addressItem.value.provinceStr ?? '', 
      cityStr: controller.addressItem.value.cityStr ?? '', 
      districtStr: controller.addressItem.value.districtStr ?? '', 
      address: controller.addressItem.value.address ?? '', 
      id: controller.addressItem.value.id!
    ), () {
      Get.toNamed(
        '${AppRouters.COMMON_PAGE}${AppRouters.MY_INFO_PAGE}${AppRouters.ADDRESS_LIST_PAGE}',
        arguments: {
          'isSelectAddress': true
        }
      )!.then((res) {
        if (res != null) {
          controller.addressItem.value = res;
          controller.update();
        } else {
          controller.getAddressList();
        }
      });
    });
  }

  /// 商品信息
  Widget _goodsInfo(String title, String headImage, String price, int quantity, String specStr, String subtotal) {
    return Container(
      color: bgWhiteColor,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(height: 12.h),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 16.w),
              child: Text(
              '魔法星选直营',
              style: TextStyle(
                color: textBlackColor,
                fontSize: 14.sp,
                fontWeight: FontWeight.bold
              ),
            ),
          ),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
            decoration: BoxDecoration(
              border: Border(
                bottom: BorderSide(
                  color: lineColor,
                  width: .5
                )
              )
            ),
            child: Row(
              children: [
                NetworkImageUtil(
                  url: headImage, 
                  width: 90.w, 
                  height: 90.w,
                  borderRadius: 5.w,
                ),
                SizedBox(width: 12.w),
                Expanded(child: Container(
                  constraints: BoxConstraints(
                    minHeight: 90.w
                  ),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Expanded(child: Container(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              title,
                              overflow: TextOverflow.ellipsis,
                              maxLines: 2,
                              style: TextStyle(
                                color: textBlackColor,
                                fontSize: 14.sp
                              ),
                            ),
                            SizedBox(height: 6.h),
                            Text(
                              specStr,
                              style: TextStyle(
                                color: textSubtitleColor,
                                fontSize: 12.sp
                              ),
                            )
                          ],
                        ),
                      )),
                      SizedBox(width: 12.w),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          Text(
                            '¥$price',
                            style: TextStyle(
                              color: textBlackColor,
                              fontSize: 12.sp
                            ),
                          ),
                          Text(
                            'x$quantity',
                            style: TextStyle(
                              color: textSubtitleColor,
                              fontSize: 12.sp,
                              fontWeight: FontWeight.bold
                            ),
                          )
                        ],
                      )
                    ],
                  )
                ))
              ],
            ),
          ),
          Container(
            alignment: Alignment.centerRight,
            padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
            child: Text.rich(
              TextSpan(
                text: '共$quantity件商品  小计: ',
                style: TextStyle(
                  color: textBlackColor,
                  fontSize: 14.sp
                ),
                children: [
                  TextSpan(
                    text: '¥',
                    style: TextStyle(
                      color: mainTextColor,
                      fontSize: 14.sp,
                      fontWeight: FontWeight.bold
                    )
                  ),
                  TextSpan(
                    text: '$subtotal',
                    style: TextStyle(
                      color: mainTextColor,
                      fontSize: 18.sp,
                      fontWeight: FontWeight.bold
                    )
                  )
                ]
              )
            ),
          )
        ],
      ),
    );
    
    // Column(
    //   crossAxisAlignment: CrossAxisAlignment.start,
    //   children: [
        
    //   ],
    // );
  }

  Widget _goodsInfoItem(String title, String value) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            title,
            style: TextStyle(
              color: textBlackColor,
              fontSize: 14.sp
            ),
          ),
          Text(
            '¥$value',
            style: TextStyle(
              color: textBlackColor,
              fontSize: 14.sp,
              fontWeight: FontWeight.bold
            ),
          )
        ],
      ),
    );
  }

}